{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 圆周率 π"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "圆周率 $\\pi$ 可能是最广为人知的无理数了。无理数英文 Irrational numbers，语义为没有道理。但更深一步，Irrational 的词根 ratio 来自希腊语比例。有理数更准确的指能写能比率(分数)形式的数字，无理数则不可以写成分数形式。\n",
    "\n",
    "复数记为$\\mathbb{C}$ 实数记为$\\mathbb{R}$ 有理数记为 $\\mathbb{Q}$ 整数记为 $\\mathbb{Z}$ 自然数记为 $\\mathbb{N}$ 包含关系如下：\n",
    "\n",
    "![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/NumberSetinC.svg/500px-NumberSetinC.svg.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "π 是圆的周长和直径的比\n",
    "\n",
    "$C = \\pi d = 2\\pi r$ \n",
    "\n",
    "$S = \\pi * (d/2)^2 =  \\pi r^2$\n",
    "\n",
    "\n",
    "关于圆周率的计算公式很多，这里只讲一个 π 的莱布尼茨公式\n",
    "\n",
    "$\\displaystyle {\\frac {\\pi }{4}}=\\sum _{n=0}^{\\infty }\\,{\\frac {(-1)^{n}}{2n+1}}$\n",
    "\n",
    "展开后即\n",
    "\n",
    "$\\displaystyle \\frac {\\pi}{4} = \\frac{1}{1} - \\frac {1}{3} + \\frac {1}{5} - \\frac {1}{7} + \\frac {1}{9} + \\cdots$\n",
    "\n",
    "使用这个公式，我们可以计算出 $\\frac {\\pi}{4}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415916535897743\n"
     ]
    }
   ],
   "source": [
    "quarterPI = 0\n",
    "MAX = 100_0000\n",
    "for n in range(MAX):\n",
    "  quarterPI += ((-1) ** n) / (2 * n + 1)\n",
    "\n",
    "print(quarterPI * 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "let quarterPI = 0\n",
    "const MAX = 100_0000\n",
    "for (let n = 0; n < MAX; ++n) {\n",
    "  quarterPI +=  ((-1) ** n) / (2 * n + 1)\n",
    "}\n",
    "console.log(quarterPI * 4)\n",
    "3.1415916535897743\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，我们进行了 100 万次求和，只精确到小数点后 5 位，这是一个不高效的算法。关于 $\\pi$ 的公式还有很多，比如 [楚德诺夫斯基算法](https://zh.wikipedia.org/zh-cn/%E6%A5%9A%E5%BE%B7%E8%AF%BA%E5%A4%AB%E6%96%AF%E5%9F%BA%E7%AE%97%E6%B3%95) ，公式就不放出来了，避免劝退。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\pi$ 的应用非常广泛，除了圆、球、三角等几何图形，甚至还离奇出现在了概率论、物理学中。后面的章节会再次提到。如果有一天当人类发现了外星人，他们世界也一定会有 $\\pi$。只是可能叫法不一样，不用 10 进制表示，或者用 $\\frac{\\pi}{4}$、 $2\\pi$ 做为基准单位，但数学上一定是完全等价的。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
